<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace App\Model;

use App\Base\BaseModel;
use App\Base\IRI;
use App\Entity\Category;
use App\Entity\ComCategory;
use App\Model\Common\CategoryCommonModel;
use Doctrine\Bundle\DoctrineBundle\Registry;
use App\Setting\CategorySetting;
use Symfony\Component\HttpFoundation\Request;

class CategoryModel extends BaseModel{


    /*
     * 列表基本设置【通用模块必须】
     */
    public function listSettingDefault()
    {
        $settings = new CategorySetting($this->getDoctrine());
        return $settings->listConfig();
    }

    //获取所有分类
    public function getAllCategory(){
        return $this->getRep()->findBy(array("parent"=>null),array("sort"=>"ASC"));
    }

    //获取所有分类并构建分类树
    public function getAllCategoryTree()
    {
        $root = $this->getAllCategory();
        $this->createTree($root,$list);
        $data = array();
        $levels = [];
        /**@var $en Category */
        foreach ($list as $en) {

            if (!$en->getParent()){
                $level = 0;
            }else{
                $level = $levels[$en->getParent()->getId()] + 1;
            }
            $levels[$en->getId()] = $level;
            $prefix = "";
            if ($level > 0) {
                $prefix .="├";
                for ($i=0;$i<$level;$i++ ) {
                    $prefix .="────";
                }
            }
            $data[] = [
                "level" => $level,
                "data" => $en,
                "pre" => $prefix,
            ];
        }
//        dd($data);
        return $data;
    }

    function createTree($objectList , &$rst = [] ,$level = 0)
    {
        foreach ($objectList as $parent) {
            $rst[] = $parent;
            if ($sons= $parent->getSons()) {
                $this->createTree($sons,$rst,$level+1);
            }
        }
    }

    //获取第一层分类
    public function getRootCategory()
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")
            ->where("level = ?")
            ->setParameter(0,"1")
            ->from("Category");
        return $qb->execute()->fetchAll();
    }

    public function getSons($parent)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("Category")
            ->where("Category.parent = ?0")
            ->setParameter(0,$parent)
            ->orderBy("Category.sort","ASC")
            ->from("App:Category","Category");
        return $qb->getQuery()->execute();
    }

    public function getSons4Setting($parent=null)
    {

        $data = $this->getAllCategoryTree();
        foreach ($data as $row) {
            if (!$parent) {
                $return[ IRI::createIRIStringFromEntity(Category::class,$row["data"]->getId())] = $row["pre"] . $row["data"]->getName();
            }else{
                if ($row["data"]->getParent() && $row["data"]->getParent()->getId() == $parent) {
                    $return[ IRI::createIRIStringFromEntity(Category::class,$row["data"]->getId())] = $row["pre"] . $row["data"]->getName();
                }
            }
        }
        return $return ;
    }

    public function getSons4Article()
    {

        $data = $this->getAllCategoryTree();
//        dd($data);
        foreach ($data as $row) {
            if ($row["data"]->getId() == 1) {
                continue;
            }
            if ($row["data"]->getParent()) {
                $return[ IRI::createIRIStringFromEntity(Category::class,$row["data"]->getId())] = $row["pre"] . $row["data"]->getName();
            }
        }
        return $return ;
    }

    /*
     * 通用Rep
     */
    public function getRep()
    {
        $settings = new CategorySetting($this->getDoctrine());
        return $this->em->getRepository($settings->bundleName.":".$settings->entityName);
    }

}